<?xml version="1.0"?>
<xsl:stylesheet xmlns:a="http://dita.oasis-open.org/architecture/2005/" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" exclude-result-prefixes="xsl a" xml:space="default">
  <xsl:output encoding="us-ascii" method="html" omit-xml-declaration="yes"/>
  <xsl:param name="filename"/>
  <xsl:param name="dirname"/>

  <xsl:template name="head-details">
    <thead>
      <th>Document Title</th>
      <th class="navigation-file-type-header">Type</th>
    </thead>
  </xsl:template>


<!-- FIXME take in more than just topicref for learning* content -->
  <xsl:template match="map">
    <div class="navigation-map">
      <xsl:call-template name="title"/>
      <xsl:variable name="refs" select="topicref | topicref//topicref[@href and not(@format = 'html') and not(ancestor::reltable)]"/>
      <xsl:if test="count($refs) &gt; 0">
        <table class="navigation-file-list" cellspacing="0">
          <xsl:call-template name="head-details"/>
          <tbody>
            <xsl:apply-templates select="$refs" /> 
          </tbody>
        </table>
      </xsl:if>
    </div>

  </xsl:template>

  <xsl:template name="title">
    <!-- self::map -->
    <h4 class="navigation-map-heading">
      <div class="navigation-map-label">Map</div>
      <a href="view-xopus-island.php?fileId={$filename}&amp;fileType=learningMap" onclick="return MapNavigation.openDocument(this, this.href);">
        <xsl:if test="not(title)">
          <xsl:value-of select="$filename"/>
        </xsl:if>
        <xsl:apply-templates select="title"/>
      </a>
    </h4>
  </xsl:template>
  
  <xsl:template match="topicref">
    <xsl:variable name="topicpath" select="concat($dirname, '/', @href)"/>
    <xsl:if test="not(contains($topicpath, ':')) and $topicpath != ''">
      <!-- skips 'http:', ads, ... -->
      <xsl:variable name="topicdoc" select="document($topicpath)"/>
      <xsl:variable name="type" select="local-name($topicdoc/*)"/>
      <xsl:variable name="title">
        <xsl:choose>
          <xsl:when test="@navtitle">
            <xsl:value-of select="@navtitle"/>
          </xsl:when>
          <xsl:otherwise>
            <xsl:value-of select="$topicdoc/*/title[1]"/>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:variable>
      <tr id="mapnav_{ generate-id() }">
        <td class="navigation-file-title-cell">
          <a href="view-xopus-island.php?fileId={@href}&amp;fileType={$type}" onclick="return MapNavigation.openDocument(this, this.href);" title="{$title}">
            <xsl:value-of select="$title"/>
            <xsl:if test="not($title)"><i>(no title)</i></xsl:if>
          </a>
        </td>
        <td class="navigation-file-type-cell">
          <xsl:value-of select="$type"/>
        </td>
      </tr>
    </xsl:if>
  </xsl:template>
  
  <xsl:template match="recent-files">
    <xsl:apply-templates select="create-form"/>
    <h4 class="navigation-heading">Recent documents</h4>
    <table class="recent-files" cellspacing="0">
      <xsl:call-template name="head-details"/>
      <tbody>
        <xsl:apply-templates select="file"/>
      </tbody>
    </table>
  </xsl:template>
  
  <xsl:template match="create-form">
    <div class="form-create-panel">
      <div onclick="return CreateDocument.toggleDialog();" class="form-create-toggle">New Document</div>
      <form action="view-navigation.php" onsubmit="return CreateDocument.submitHandler()" method="post" id="createDocumentForm" class="form-create form-create-has-newdoc-{boolean(newdoc)}">
        <xsl:attribute name="style">
          <xsl:choose>
            <xsl:when test="new-doc">display:block;</xsl:when>
            <xsl:otherwise>display:none;</xsl:otherwise>
          </xsl:choose>
        </xsl:attribute>
        <div>
          <div class="form-create-field valid-field-true">
            <label for="name">Title</label>
            <input tabindex="21" type="text" name="title" id="name" value="" class="form-create-field-panel"/>
          </div>
          <div class="form-create-field valid-field-true">
            <label for="id">ID</label>
            <input tabindex="22" type="text" id="id" name="id" value="" class="form-create-field-panel"/>
          </div>
          <div class="form-create-field valid-field-true">
            <label for="type">Type</label>
            <select tabindex="23" id="type" name="type" class="form-create-field-panel">
              <option value="">Choose type&#x2026;</option>
              <optgroup label="DITA Core">
                <option value="concept.dita">Concept</option>
                <option value="map.ditamap">Map</option>
                <option value="reference.dita">Reference</option>
                <option value="task.dita">Task</option>
                <option value="topic.dita">Topic</option>
              </optgroup>
              <optgroup label="DITA Learning">
                <option value="learningAssessment.dita">Assessment</option>
                <option value="learningContent.dita">Content</option>
                <option value="learningOverview.dita">Overview</option>
                <option value="learningPlan.dita">Plan</option>
                <option value="learningSummary.dita">Summary</option>
              </optgroup>
            </select>
          </div>
          <div class="form-create-field valid-field-true">
            <label for="use-sample-checkbox" class="form-create-checkbox-label"><input type="checkbox" id="use-sample-checkbox" name="use-sample" value="1" class="form-create-checkbox" onclick="return CreateDocument.saveUseSample(this.checked)"><xsl:attribute name="checked"><xsl:if test="create-form/use-sample">checked</xsl:if></xsl:attribute></input>
              Use sample content
            </label>
          </div>
          <div class="form-create-submitrow">
            <input type="hidden" name="cmd" value="create-document"/>
            <input tabindex="25" type="reset" onclick="return CreateDocument.cancelDialog();" value="Cancel"/>
            <input tabindex="24" type="submit" value="Create"/>
          </div>
        </div>
        <xsl:apply-templates select="new-doc"/>
      </form>
    </div>
  </xsl:template>

  <xsl:template match="new-doc">
    <div id="createDocumentNewDoc" class="form-create-newdoc-panel">
      <a href="view-xopus-island.php?fileId={@href}&amp;fileType={@type}" onclick="MapNavigation.openDocument(this, this.href); CreateDocument.removeNewDoc(); return false;">
        <xsl:text>Open new document &#x201C;</xsl:text>
        <xsl:value-of select="text()"/>
        <xsl:text>&#x201D;</xsl:text>
      </a>
    </div>
  </xsl:template>

  <xsl:template match="recent-files/file">
    <tr>
      <td>
        <a href="view-xopus-island.php?fileId={@href}&amp;fileType={@type}" onclick="return MapNavigation.openDocument(this, this.href);">
          <xsl:apply-templates/>
          <xsl:if test="not(text())"><i>(no title)</i></xsl:if>
        </a>
      </td>
      <td class="navigation-file-type-cell">
        <xsl:value-of select="@type"/>
      </td>
    </tr>
  </xsl:template>
</xsl:stylesheet>
